// const { DefinePlugin } = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')

module.exports = {
  mode: 'development',
  entry: './08-vue/main.js',
  // watch: true, // watch for changes
  output: {
    filename: 'main.js',
    path: __dirname + '/dist', // __dirname is the current directory
    // assetModuleFilename: 'images/[name][ext]'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.css$/,
        // use: ['style-loader', 'css-loader', 'postcss-loader'],
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              esModule: false, //
              importLoaders: 1,
            },
          },
          {
            loader: 'postcss-loader',
          },
        ],
      },
      {
        test: /\.less$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'less-loader',
          },
        ],
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'sass-loader',
          },
        ],
      },
      {
        test: /\.(png|svg|jpe?g|gif)$/,
        // file-loader、url-loader、asset 选其一
        // use: [
        // {
        //   loader: 'file-loader',
        //   options: {
        //     // esModule: false,
        //     name: '[name].[hash:6].[ext]',
        //     outputPath: 'images/',
        //   },
        // },
        // {
        //   loader: 'url-loader', // url-loader 可以将图片转为 base64
        //   options: {
        //     name: '[name].[hash:6].[ext]',
        //     outputPath: 'images/',
        //     limit: 1024 * 10, // 10kb
        //   },
        // },
        // ],
        type: 'asset', // type: 'asset/resource',  // type: 'asset/inline',
        generator: {
          filename: 'images/[name].[hash:6][ext]', // 图片资源的输出文件名
        },
        parser: {
          dataUrlCondition: {
            maxSize: 1024 * 10, // 10kb 以下的图片转换为 base64，10kb 以上的转换为资源文件
          },
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        // file-loader、url-loader、asset 选其一
        // use: ['file-loader'],
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[name].[hash:6][ext]', // 字体资源的输出文件名
        },
      },
      {
        test: /\.vue$/,
        use: ['vue-loader'],
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(), // 清除 dist 文件夹
    new HtmlPlugin({
      title: 'Hello Webpack 5',
      filename: 'index.html',
      favicon: './public/favicon.ico',
      template: './public/index.html',
      minify: {
        // removeAttributeQuotes: true, // 去掉属性的双引号
        // collapseWhitespace: true, // 去掉空格
        removeComments: true, // 去掉注释
      },
    }),
    new CopyPlugin({
      patterns: [
        {
          from: 'public',
          globOptions: {
            ignore: ['**/index.html', '**/favicon.ico'],
          },
          // to: 'images',
        },
      ],
    }),
  ],
  devServer: {
    hot: true, // 开启热更新
    // contentBase: './dist', // 告诉服务器从哪里提供内容
    port: 7000, // 设置端口号
  },
  target: 'web', // 开启 webpack-dev-server
}
